package org.jpro.modules.system.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jpro.common.jpa.Specifications;
import org.jpro.modules.system.dao.DictRepository;
import org.jpro.modules.system.entity.Dict;
import org.jpro.modules.system.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <P>功能描述 : 字典service实现 </P>
 * <P>File name : DictServiceImpl </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/3 下午11:58</P>
 */
@Service
@Transactional(readOnly = true)
public class DictServiceImpl implements IDictService {

    @Autowired
    private DictRepository dictRepository;

    @Override
    public Page<Dict> search(String label, String value, String type, PageRequest pageRequest) {
        Specification<Dict> specification = Specifications.<Dict> and().like(StringUtils.isNoneBlank(label), "label", "%" + label + "%")
                .like(StringUtils.isNoneBlank(value), "value", "%" + value + "%").like(StringUtils.isNoneBlank(type), "type", "%" + type + "%")
                .build();

        return dictRepository.findAll(specification, pageRequest);
    }

    @Override
    @Transactional
    public void save(Dict dict) {
        dictRepository.save(dict);
    }

    @Override
    public Dict get(Integer id) {
        return dictRepository.getOne(id);
    }

    @Override
    @Transactional
    public void update(Dict dict) {
        dictRepository.save(dict);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        dictRepository.delete(id);
    }
}
